<script setup lang="ts">
import { provide, ref, watch } from 'vue'
import { RouterView } from 'vue-router'
import AppLayout from './components/AppLayout.vue'
import { useAppStore } from './stores/app'

const appStore = useAppStore()
const theme = ref(appStore.theme)

// 检测并应用主题变更
watch(() => appStore.theme, (newTheme) => {
  theme.value = newTheme
  // 更新body上的类以应用全局主题
  if (newTheme === 'dark') {
    document.body.classList.add('dark-mode')
  } else {
    document.body.classList.remove('dark-mode')
  }
})

// 初始应用主题
if (theme.value === 'dark') {
  document.body.classList.add('dark-mode')
} else {
  document.body.classList.remove('dark-mode')
}

// 提供主题给子组件
provide('theme', theme)
</script>

<template>
  <AppLayout>
    <RouterView />
  </AppLayout>
</template>

<style>
/* 导入CSS变量 */
@import './assets/css/variables.css';

/* 全局样式 */
html, body {
  height: 100%;
  margin: 0;
  padding: 0;
  font-family: var(--font-family);
  color: var(--text-color);
  background-color: var(--bg-color);
}

#app {
  height: 100%;
  overflow: hidden;
}

.dark-mode {
  color: var(--text-color);
  background-color: var(--bg-color);
}

:root {
  font-synthesis: none;
  text-rendering: optimizeLegibility;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  -webkit-text-size-adjust: 100%;
}

/* 文本选择颜色 */
::selection {
  background-color: var(--primary-color);
  color: #fff;
}

/* 滚动条样式 */
::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}

::-webkit-scrollbar-thumb {
  background-color: var(--border-color);
  border-radius: 4px;
}

::-webkit-scrollbar-track {
  background-color: transparent;
}

/* 过渡动画 */
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}

/* 全局边框盒模型 */
* {
  box-sizing: border-box;
}
</style>
